package com.yinshu.teleshield.tkbase.controller;

import cn.hutool.core.collection.CollUtil;
import com.yinshu.teleshield.tkbase.base.R;
import com.yinshu.teleshield.tkbase.base.entity.TKSuperEntity;
import com.yinshu.teleshield.tkbase.service.SuperCacheService;
import com.yinshu.teleshield.utils.CommonBeanUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * SuperCacheController
 * <p>
 * 继承该类，在SuperController类的基础上扩展了以下方法：
 * 1，get ： 根据ID查询缓存，若缓存不存在，则查询DB
 *
 * @author yangjunxiong
 */
public abstract class SuperCacheController<S extends SuperCacheService<Id, Entity>,
        Id extends Serializable, Entity extends TKSuperEntity<Id>, SaveVO, UpdateVO, PageQuery, ResultVO>
        extends SuperController<S, Id, Entity, SaveVO, UpdateVO, PageQuery, ResultVO> {
    Logger log = LoggerFactory.getLogger(SuperCacheController.class);

    @Override
    public SuperCacheService<Id, Entity> getSuperService() {
        return superService;
    }

    /**
     * 单体查询
     *
     * @param id 主键id
     * @return 查询结果
     */
    @Override
    public R<ResultVO> get(@PathVariable Id id) {
        log.info("{}.get.缓存单体查询,请求参数:id:{}", this.getClass().getName(), id);
        Entity entity = getSuperService().getByIdCache(id);
        ResultVO resultVO = CommonBeanUtils.toBean(entity, getResultVOClass());
        if (Objects.isNull(resultVO)) {
            return success(resultVO);
        }
        return success(resultVO);
    }

    /**
     * 查询详情
     *
     * @param id 主键id
     * @return 查询结果
     */
    @Override
//    @WebLog("'查询:' + #id")
    public R<ResultVO> getDetail(@PathVariable Id id) {
        log.info("{}.getDetail.缓存查询详情,请求参数:id:{}", this.getClass().getName(), id);
        Entity entity = getSuperService().getByIdCache(id);
        ResultVO resultVO = CommonBeanUtils.toBean(entity, getResultVOClass());
        if (Objects.isNull(resultVO)) {
            return success(resultVO);
        }
        this.getSuperService().handlerListResult(Collections.singletonList(resultVO));
        return success(resultVO);
    }

    @Override
//    @WebLog("根据Ids批量查询")
    public R<List<ResultVO>> findByIds(@RequestBody List<Id> ids) {
        log.info("{}.findByIds.缓存根据Ids批量查询,请求参数:ids:{}", this.getClass().getName(), CommonBeanUtils.toJSONString(ids));
        if (CollUtil.isEmpty(ids)) {
            return R.success(Collections.emptyList());
        }
        List<Entity> list = getSuperService().findByIds(ids,null);
        if (CollUtil.isEmpty(list)) {
            return R.success(Collections.emptyList());
        }
        List<ResultVO> resultVOs = CommonBeanUtils.toList(getResultVOClass(), list);
        this.getSuperService().handlerListResult(resultVOs);
        return success(resultVOs);
    }

    /**
     * 刷新缓存
     *
     * @return 是否成功
     */
    @Operation(summary = "刷新缓存", description = "刷新缓存")
    @PostMapping("refreshCache")
//    @WebLog("'刷新缓存'")
    public R<Boolean> refreshCache(@RequestBody List<Long> ids) {
        log.info("{}.refreshCache.刷新缓存,请求参数:ids:{}", this.getClass().getName(), CommonBeanUtils.toJSONString(ids));
        getSuperService().refreshCache(ids);
        return success(true);
    }

    /**
     * 清理缓存
     *
     * @return 是否成功
     */
    @Operation(summary = "清理缓存", description = "清理缓存")
    @PostMapping("clearCache")
//    @WebLog("'清理缓存'")
    public R<Boolean> clearCache(@RequestBody List<Long> ids) {
        log.info("{}.clearCache.清理缓存,请求参数:ids:{}", this.getClass().getName(), CommonBeanUtils.toJSONString(ids));
        getSuperService().clearCache(ids);
        return success(true);
    }
}
